package jam.UserInterface.UI;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.io.File;

import javax.imageio.ImageIO;
import javax.swing.*;

public class uploadAvatarWindow extends JFrame{

    // this code was taken from the book Java Swing 2nd edition by Marc Loy, Robert Eckstein, Dave Wood, James Elliott and Brian Cole, published in 2002 by O'Reilly, USA.

	public uploadAvatarWindow(JPanel picPanel, JLabel pic){
				
		JFileChooser chooser = new JFileChooser();
		int option = chooser.showSaveDialog(uploadAvatarWindow.this);
		if(option==JFileChooser.APPROVE_OPTION){			
			if(chooser.getSelectedFile()!=null){
				Image myPicture;
				try {
					//chooser.getSelectedFile().getAbsoluteFile(), chooser.getSelectedFile().getName())
					String name = chooser.getSelectedFile().getAbsolutePath();
					myPicture = ImageIO.read(new File(name));
					myPicture = myPicture.getScaledInstance(40, 40, 0);
					JLabel picLabel = new JLabel(new ImageIcon(myPicture));

					picPanel.remove(pic);
					GridBagConstraints c = new GridBagConstraints();
					
					c.fill = GridBagConstraints.BOTH;
					c.gridx = 0;
					c.gridy = 0;
					c.weightx = 0.5;
					c.weighty = 0.5;
					
					picPanel.add(picLabel,c);
					picPanel.repaint();
					picPanel.revalidate();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}	
			}			
		}
	}
}

